﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using NHibernate;
using NHibernate.Cfg;
using System.IO;

namespace DIST.LR.ZHJG.Pgybc.WCF.Manager
{
    public sealed class NHibernateHelper
    {
        private readonly string path = System.AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["hibernatecfg"].ToString();
        private readonly string logpath = System.AppDomain.CurrentDomain.BaseDirectory + "log4net.cfg.xml";

        private static ISessionFactory _sessionFactory;
        private static NHibernateHelper _instance;
        public static NHibernateHelper Instance()
        {
            if (_instance != null)
            {
                return _instance;
            }
            {
                _instance = new NHibernateHelper();
                return _instance;
            }
        }

        private NHibernateHelper()
        {
            _sessionFactory = GetSessionFactory();
            log4net.Config.XmlConfigurator.Configure(new FileInfo(logpath));
        }

        private ISessionFactory GetSessionFactory()
        {
            if (_sessionFactory != null)
            {
                return _sessionFactory;
            }
            {
                _sessionFactory = (new NHibernate.Cfg.Configuration()).Configure(path).BuildSessionFactory();
                return _sessionFactory;
            }
        }

        public ISession GetSession() 
        {
            return _sessionFactory.OpenSession();
        }
    }
}